import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'

from google.appengine.dist import use_library
use_library('django', '1.2')

from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp.util import run_wsgi_app

import cssi_data

class Student(object):
  def __init__(self, name, full_name, buddy=None, photo=None):
    self.name = name
    self.full_name = full_name
    self.buddy = buddy
    self.photo = photo

class HomeHandler(webapp.RequestHandler):
  def get(self):
    students = []
    for name in cssi_data.STUDENTS:
      student = Student(name, cssi_data.FULL_NAMES[name])
      students.append(student)

    template_values = {'students': students}
    path = os.path.join(os.path.dirname(__file__), 'home.html')
    self.response.out.write(template.render(path, template_values))

class ProfileHandler(webapp.RequestHandler):
  def get(self):
    name = self.request.get('name')
    template_values = {}

    if name in cssi_data.STUDENTS:
      student = Student(name, cssi_data.FULL_NAMES[name], cssi_data.BUDDIES[name],   cssi_data.PHOTOS.get(name))
      template_values['student'] = student

      template_file = 'profile.html'

    else:
      self.error(404)
      template_file = 'profile_error.html'

    path = os.path.join(os.path.dirname(__file__), template_file)
    self.response.out.write(template.render(path, template_values))

def main():
  application = webapp.WSGIApplication([('/', HomeHandler),
                                        ('/profile.*', ProfileHandler)],
                                       debug=True)
  run_wsgi_app(application)

if __name__ == '__main__':
  main()
